package com.commons;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.servlet.ServletContext;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.nmc.core.manager.EmployeeManager;
import com.nmc.core.manager.FeedbackManager;
import com.nmc.core.manager.TrackManager;
import com.nmc.core.manager.WardManager;
import com.nmc.core.manager.ZoneManager;
import com.nmc.model.Employee;
import com.nmc.model.Feedback;
import com.nmc.model.Ward;
import com.nmc.model.Zone;


/**
 * 
 * @author uday.kumar
 *
 */


@SuppressWarnings("unchecked")
public class AjaxHelper implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6565834038151689201L;
	protected static ServletContext context = null;
	
	/*
	 * represent the defualt lang Code
	 */
	protected final String langCode = "EN";

	
	/**
	 * The constructor which takes the servlet context as parameter.
	 * 
	 * @param context context
	 */
	public AjaxHelper(ServletContext context) {
		AjaxHelper.context = context;
		//System.out.println("AjaxHelper***");
	}

	/**
	 * Default constructor.
	 * 
	 */
	public AjaxHelper() {

	}

	/**
	 * 
	 * @param wardId
	 * @return
	 */
	public Collection getZoneList() {

		ApplicationContext ctx = WebApplicationContextUtils
				.getRequiredWebApplicationContext(context);
		ZoneManager zoneManager =(ZoneManager) ctx.getBean("zoneManager");
		
		List list = new ArrayList();
		
			List actList = new ArrayList();
			try {
				list = zoneManager.getZoneList();
				
				
				ListFieldWrapper aw = new ListFieldWrapper();
				for(int i=0;i<list.size();i++){
					
					Zone zone = (Zone)list.get(i);
					
					aw = new ListFieldWrapper(""+zone.getId(),zone.getLatLong(),zone.getName());
					
				 	actList.add(aw);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		return actList;
	}
	
	/**
	 * 
	 * @param wardId
	 * @return
	 */
	public Collection getWardList(int zoneId) {

		//System.out.println("Ajax getWardList(zoneId)");
		ApplicationContext ctx = WebApplicationContextUtils
				.getRequiredWebApplicationContext(context);
		WardManager wardManager =(WardManager) ctx	.getBean("wardManager");
		
		List list = new ArrayList();
		
			list = wardManager.getWardList(zoneId);
			
			List actList = new ArrayList();
			ListFieldWrapper aw = new ListFieldWrapper();
			for(int i=0;i<list.size();i++){
				
				Ward ward = (Ward)list.get(i);
				
				aw = new ListFieldWrapper(""+ward.getId(),ward.getName());
				
			 	actList.add(aw);
			}
		
		return actList;
	}
	
	public Ward getWard(int wardId){
		ApplicationContext ctx = WebApplicationContextUtils
		.getRequiredWebApplicationContext(context);
		WardManager wardManager =(WardManager) ctx	.getBean("wardManager");
		Ward ward = wardManager.getWard(wardId);
	//	System.out.println("ward Track List "+getWardTrackList(wardId));
		return ward;
	}
	public boolean saveFeedback(String feedbackText){
		ApplicationContext ctx = WebApplicationContextUtils
		.getRequiredWebApplicationContext(context);
		FeedbackManager feedbackManager =(FeedbackManager) ctx	.getBean("feedbackManager");
		Feedback feedback = new Feedback();
		feedback.setFeedbackText(feedbackText);
		feedbackManager.insertFeedback(feedback);
		
		return true;
	}
	
	public Collection getWardTrackList(int wardId)
	{
			ApplicationContext ctx = WebApplicationContextUtils
			.getRequiredWebApplicationContext(context);
			TrackManager trackManager =(TrackManager)ctx.getBean("trackManager");
			List list = new ArrayList();
			list = trackManager.getWardTrackList(wardId);
			return list;
	}

	public boolean validateEmp(String empId){
		ApplicationContext ctx = WebApplicationContextUtils
		.getRequiredWebApplicationContext(context);
		EmployeeManager employeeManager =(EmployeeManager) ctx	.getBean("employeeManager");
		Employee employee = employeeManager.getEmployeeById(empId);
		if(employee == null){
			return false;
		}
		return true;
	}


	public Collection getWardTrackListByEmp(int id)
	{
			ApplicationContext ctx = WebApplicationContextUtils
			.getRequiredWebApplicationContext(context);
			TrackManager trackManager =(TrackManager)ctx.getBean("trackManager");
			List list = new ArrayList();
			list = trackManager.getWardTrackListByEmp(id);
			return list;
	}


	

}


